package com.mybatisplus.demo.android.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mybatisplus.demo.android.dao.PasswordMapper;
import com.mybatisplus.demo.android.dao.UserMapper;
import com.mybatisplus.demo.android.enmu.ApiResponse;
import com.mybatisplus.demo.android.enmu.ResultCode;
import com.mybatisplus.demo.android.model.Password;
import com.mybatisplus.demo.android.model.User;
import com.mybatisplus.demo.android.service.RegisterService;
import com.mybatisplus.demo.android.viewobject.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 注册
 */
@Service
public class RegisterServiceimpl implements RegisterService {

    @Autowired
    private UserMapper userMapper;//与dao层进行交互

    @Autowired
    private PasswordMapper passwordMapper;


    //将核心模型转化为UI可用模型
    private UserVo convertFromModel(User userModel) {
        if (null == userModel) {
            return null;
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(userModel, userVo);
        return userVo;
    }

    @Override
    public ApiResponse register(String user_id, String password, String code) {

        ApiResponse apiResponse = new ApiResponse();
        //查询用户是否存在
        User userSelect = new User();
        userSelect.setUserId(user_id);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(userSelect);
        User user1 = userMapper.selectOne(queryWrapper);

        if (null == user1) {
            userSelect.setCreate_date("" + LocalDateTime.now());
            userSelect.setUpdate_date("" + LocalDateTime.now());
            userSelect.setUserId(user_id);
            userSelect.setTelphone(user_id);
            userSelect.setPassword(password);
            userSelect.setUser_name("默认");
            //插入用户
            int i = userMapper.insert(userSelect);
            if (i > 0) {
                //新建密码
                Password ps = new Password();
                ps.setPassword(userSelect.getPassword());
                ps.setUserId(userSelect.getUserId());
                ps.setCreate_date("" + LocalDateTime.now());
                ps.setUpdate_date("" + LocalDateTime.now());
                int p = passwordMapper.insert(ps);
                if (p > 0) {
                    //新建成功
                    apiResponse.setResultCode(ResultCode.SUCCESS);
                    apiResponse.setMessage("创建成功");
                } else {
                    //新建密码失败
                    apiResponse.setResultCode(ResultCode.PASSWORD_CREATE_ERROR);
                }
            } else {
                //新建用户失败
                apiResponse.setResultCode(ResultCode.USER_CREATE_ERROR);
            }

        } else {
            //用户已存在
            apiResponse.setResultCode(ResultCode.USER_ID_ISEXIST);
        }

        return apiResponse;
    }
}
